<?php
// +----------------------------------------------------------------------
// | 有理想的地方，地狱都是天堂。
// +----------------------------------------------------------------------
// | Copyright @ df81.com 版权所有
// +----------------------------------------------------------------------
// | 作者: 南国老符 邮箱：1062129401@qq.com
// +----------------------------------------------------------------------

namespace app\admin\model;
use think\Model;
use think\facade\Db;

class User extends Model{
    /**列表
    * lang 语言
    * group_id 会员组
    * signid 1正常9已删除，大于9就是删除时间戳
    * listNum 每页显示数量
    * keyword 关键词
    * order 排序
    */
	static function index($lang,$group_id,$signid=1,$listNum,$keyword,$order){
		$where[] = ['user.id','>',0];
		if(!empty($group_id)) $where[] = ['auth_group.id','=',$group_id];
		if(!empty($keyword)) $where[] = ['user.nickname|user.realname|user.mobile|user.email|user.qq|user.wechat','like',"%$keyword%"];
		if($signid > 8){//已软删除
			$where[] = ['user.signid','>','8'];
		}else{
			$where[] = ['user.signid','=',$signid];
		}

		$pageParam = array('signid'=>$signid);
        $pageParam += array('lang'=>$lang);
        $pageParam += array('group_id'=>$group_id);
        $pageParam += array('keyword'=>$keyword);

		//视图查询
		$rs = Db::view('user','*')
            ->view('auth_group_access','*','user.id=auth_group_access.uid')
            ->view('auth_group',['id'=>'Auth_group_id','signid','rules','title'],'auth_group_access.group_id=auth_group.id')
            ->where($where)->order($order)
            ->paginate([
            	'list_rows'=> $listNum,
            	'query'=> $pageParam,
            ]);
		return $rs;
	}

	//日期
    protected function setAddtimeAttr(){
		return time();
    }

	//密码
    protected function setPasswordAttr(){
		$password = input("password");
		return md5($password);
    }
	
    //新增编辑
	static function edit($id){
		$groupid = input("groupid");
		$AuthGroupAccess = new AuthGroupAccess;
	    if($id>0){//编辑
		    $password = input('password');
			$user = User::find($id);
			if(empty($password)){//密码为空时，只写入以下字段值（不更新密码字段）
			    $user->allowField(['id','username','realname','gender','wechat','qq','apply_type','distributor_id','company','telephone','birthday','areaid','question','answer','note','email','mobile','nickname','avatar','sequence'])->save(input('post.'));
			}else{
			    $user->save(input('post.'));
			}
			//更新auth_group_access表
			$AuthGroupAccess->where('uid',$user->id)->save(['group_id'=>$groupid]);
		}else{//新增
			$user = new User;
			$user->save(input('post.'));
			//添加auth_group_access表
            $AuthGroupAccess->data(['group_id'=>$groupid,'uid'=>$user->id])->save();
		}

		return $user->id;
	}
	
	/**
     * 获取用户信息 [网站及APP小程序共用]
     * userid 用户ID或用户名
     * field 字段
    */
    static function getUser($userid, $field = 'id,parentid,areaid,username,nickname,realname,spread,type,mobile,email,qq,telephone,slogan,profile,hits,score,balance,cash,subscribe,company,birthday,gender,wechat,business,contact,note,avatar,logo,banner'){
        $where[] = is_numeric($userid) ? ['user.id','=',$userid] : ['user.username','=',$userid];
        $rs = Db::view('user',$field)
            ->view('auth_group_access','uid','auth_group_access.uid=user.id')
            ->view('auth_group',['id'=>'g_id','parentid'=>'g_parentid','upgrade'=>'g_upgrade','discount'=>'g_discount','current_price'=>'g_current_price','give_price'=>'g_give_price','title'=>'g_title','signid'=>'g_signid','is_distributor','level_1','level_2','level_3'],'auth_group.id=auth_group_access.group_id')
            ->where($where)
            ->find();
        if(!empty($rs)){
            // 性别格式化
            switch ($rs['gender']) {
                case '1':
                    $rs['genderStr'] = '先生';
                    break;
                case '2':
                    $rs['genderStr'] = '女士';
                    break;
                default:
                    $rs['genderStr'] = '';
                    break;
            }
            // 类型格式化
            switch ($rs['type']) {
                case '1':
                    $rs['typeStr'] = '企业';
                    break;
                case '2':
                    $rs['typeStr'] = '个体户';
                    break;
                default:
                    $rs['typeStr'] = '个人';
                    break;
            }
            // 所在地
            $city = getParentStr($rs['areaid'], 'area', ' ', 'id,parentid,title', 'title');
            $cityArray = explode(' ',$city);
            $city = count($cityArray) > 1 ? $cityArray[1] : $city;
            $rs['city'] = $city;
        }
        
        return $rs;
    }
}